import pygame
from pygame.locals import *
from costants import *
import ranking
######################################################################
######################################################################
class Field:
	def __init__(self,screen):
		self.matrix=[]
		self.bg=pygame.image.load(TETRIS_IMAGE).convert()
		self.red=pygame.image.load(SINGLE_RED_IMAGE).convert()
		self.blue=pygame.image.load(SINGLE_BLUE_IMAGE).convert()
		self.green=pygame.image.load(SINGLE_GREEN_IMAGE).convert()
		self.yellow=pygame.image.load(SINGLE_YELLOW_IMAGE).convert()
		self.purple=pygame.image.load(SINGLE_PURPLE_IMAGE).convert()
		self.screen=screen
		self.judge=ranking.Judge(screen)
		self.empty_lines=MATRIX_HEIGHT
		for i in range(MATRIX_HEIGHT):
			self.matrix.append([])
			for j in range(MATRIX_WIDTH):
				self.matrix[i].append(0)
				
	def remove_complete_lines(self):
		deleted_lines=0
		for i in range(MATRIX_HEIGHT):
			if self.matrix[i].count(0)==0:
				self.del_line(i)
				deleted_lines+=1
		self.update_empty_lines()
                return deleted_lines
		
	def del_line(self,line_index):
		for i in range(line_index,0,-1):
			for j in range(MATRIX_WIDTH):
				self.matrix[i][j]=self.matrix[i-1][j]
				
	def full(self):
		for j in range(0,MATRIX_WIDTH):
			if self.matrix[1][j]!=0: return True
		return False
		
	def blit_field(self):
		for i in range(0,MATRIX_HEIGHT):
			for j in range(0,MATRIX_WIDTH):
				pos=(j*BLOCK_DIMENSION+TETRIS_XOFFSET,\
				     i*BLOCK_DIMENSION+TETRIS_YOFFSET)
				bg_rect=(j*BLOCK_DIMENSION,i*BLOCK_DIMENSION,\
				         BLOCK_DIMENSION,BLOCK_DIMENSION)
				if self.matrix[i][j]=='R': self.screen.blit(self.red, pos)
				if self.matrix[i][j]=='B': self.screen.blit(self.blue, pos)
				if self.matrix[i][j]=='G': self.screen.blit(self.green, pos)
				if self.matrix[i][j]=='Y': self.screen.blit(self.yellow, pos)
				if self.matrix[i][j]=='P': self.screen.blit(self.purple, pos)
				if self.matrix[i][j]==0: self.screen.blit(self.bg, pos, bg_rect)
		
	def field_status(self,deleted_lines):
		global_status=0
		for ind in range(2,len(self.matrix)-1):
			empty_ind1=[]
			empty_ind2=[]
                        ## RIEMPIO LE LISTE CON GLI INDICI DEGLI ZERI NELLE DUE LINEE CONTIGUE
                        ## 
                        try:
                                i=-1
                                while 1:
                                        i=self.matrix[ind].index(0,i+1)
                                        empty_ind1.append(i)
                        except ValueError: pass
                        try:
                                i=-1
                                while 1:
                                        i=self.matrix[ind+1].index(0,i+1)
                                        empty_ind2.append(i)
                        except ValueError: pass

                        ##PER OGNI ZERO NELLA LINEA INFERIORE CONTROLLO CHE CE NE SIA UNO
                        #NELLA LINEA SUPERIORE ALLA STESSA POSIZIONE, AI LATI NELLA LINEA 
			#SUPERIORE E AI LATI NELLA LINEA STESSA. SE SU QUESTE 5 POSIZIONI 
			#CONTROLLATE NE RISULTANO OCCUPATE 3, ALLORA VIENE CONTATO UN ERRORE 
			#DI GIOCO
                        
			for index in empty_ind2:
				count=0
                ##TROVANDOSI AI BORDI DEL CAMPO DA GIOCO CONTROLLO SOLO LA POSIZIONE 
		#SOPRASTANTE
				if index==0 or index==13:
					try:
						empty_ind1.index(index)
					except ValueError:
						count+=3
				else: 
					triplet=[index-1,index,index+1]
					try:
						empty_ind1.index(triplet[1])
					except ValueError:
						for i in triplet:
							try:
								empty_ind1.index(i)
							except ValueError:
								count+=1
							try:
								empty_ind2.index(i)
							except ValueError:
								count+=1
				if count >= 3:
					global_status+=1
		self.judge.rank(global_status,self.empty_lines,deleted_lines)
			
	def is_free(self,occuped_position):
		for y,x in occuped_position:
			try: self.matrix[y]
			except IndexError: return False
			try: self.matrix[y][x]
			except IndexError: return False
			if x<0: return False
			if self.matrix[y][x]!=0: return False
		else: return True
		
	def fill_mat(self,occuped_position, color):
		for index_couple in occuped_position:
			self.matrix[index_couple[0]][index_couple[1]]=color
			
	def update_empty_lines(self):
		empty_lines=0
		i=0
		while self.matrix[i].count(0)==MATRIX_WIDTH:
			empty_lines += 1
			i+=1
		self.empty_lines=empty_lines
####################################################################################
####################################################################################
